package impl;

import bean.Role;
import bean.UserInfo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import sql.UserSql;
import user.UserDao;
import util.DBHelper;

import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * @classname:UserDaoImpl
 * @description:
 * @author: Mr.Z
 * @date: 2020/12/30 22:41
 * @version: 0.1
 * @since:1.8
 */
public class UserDaoImpl implements UserDao {
    private Connection connection;
    public UserDaoImpl(Connection connection){
        this.connection = connection;
    }
    public UserDaoImpl(){
    }
    @Override
    public int addUsr(UserInfo userInfo) throws Exception {
        return new QueryRunner(DBHelper.getDataSource()).update(UserSql.INSERT_USER,
                userInfo.getName(), userInfo.getAge(),
                userInfo.getPass(), userInfo.getBalance(),
                userInfo.getImage(), userInfo.getBirthday(),
                userInfo.getRoleid(), userInfo.getStatus());
    }



    @Override
    public int deleteUserById(int id) throws Exception {
        return new QueryRunner(DBHelper.getDataSource()).update(UserSql.DELETE_USER_BYID, id);
    }

    @Override
    public UserInfo selectUserOneById(int id) throws Exception {
//        return new QueryRunner(DBHelper.getDataSource()).query(UserSql.SELECT_USER_BYID,new BeanHandler<>(UserInfo.class),id);
        return new QueryRunner(DBHelper.getDataSource()).query(UserSql.SELECT_USER_BYID, new BeanListHandler<>(UserInfo.class), id).get(0);
    }

    @Override
    public List<UserInfo> selectUserAll() throws Exception {
        return new QueryRunner(DBHelper.getDataSource()).query(UserSql.SELECT_USER_ALL, new BeanListHandler<>(UserInfo.class));
    }

    @Override
    public int updateUser(UserInfo userInfo) throws Exception {
        return new QueryRunner(DBHelper.getDataSource()).update(UserSql.UPDATA_USER,
                userInfo.getName(), userInfo.getAge(),
                userInfo.getPass(), userInfo.getBalance(),
                userInfo.getImage(), userInfo.getBirthday(),
                userInfo.getRoleid(), userInfo.getStatus(), userInfo.getId());
    }

    @Override
    public List<Map<String, Object>> selectRoleAndPer(int id) throws Exception {
        return new QueryRunner(DBHelper.getDataSource()).query(UserSql.SELECT_ROLE_PER, new MapListHandler(), id);
    }


//新增角色权限信息
    @Override
    public int addRoleAndPer(int roleId, int[] pids) throws Exception {
        StringBuilder builder = new StringBuilder("insert into tb_role_per (roleid,pid) values ");
        int length = pids.length;
        int count = 0;
        for (int pid : pids) {
            count++;
            builder.append("(");
            builder.append(roleId);
            builder.append(",");
            builder.append(pid);
            builder.append(")");
            if (count != length) {
                builder.append(",");
            }
        }
        return new QueryRunner().update(connection,builder.toString());
    }

    //新增角色
    @Override
    public long addRole(Role role) throws Exception {
        return new QueryRunner().insert(connection,UserSql.INSERT_ROLR,new ScalarHandler<Long>(),role.getRolename(),role.getRoledesc());
    }


    //新增用户信息
    @Override
    public long addUserBackId(UserInfo user) throws Exception {
        return new QueryRunner(DBHelper.getDataSource()).insert(UserSql.INSERT_USER,
                new ScalarHandler<Long>(),
                user.getName(), user.getAge(),
                user.getPass(), user.getBalance(),
                user.getImage(), user.getBirthday(),
                user.getRoleid(), user.getStatus());
    }
}
